package com.rose.doorto.footbathpay.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.rose.doorto.footbathpay.vo.AlipayVO;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.rose.doorto.footbathdoor.pojo.Order;

import java.math.BigDecimal;

/**
 * @Author 李庆超
 * @email 1481791489@qq.com
 * @Package：com.rose.doorto.footbathpay
 * @Project：door-to-door
 * @name：AlipayTemplate
 * @date 2024/08/09 2024/8/9:11:10:
 */
@ConfigurationProperties(prefix = "alipayss")
@Component
@Data
public class AlipayTemplateold {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public  String appId = "9021000126614730";

    // 应用私钥，就是工具生成的应用私钥
    public String merchantPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCCq1Um0ftNu+nYquF+qO50aQhOtHZMLn6mZbG2l72MoyjgpOmTDGUpzm8yRFMOAxc4DBo2mRAH2Dwzoasv9So8YxkZIkhEOLE/+EUQHywbMTq8S31kUBxCLqoGLPcfUiaTWklSy1KJKUe6FGClQp3W/ea0VdmN9eNDik6FCT5qk79nc1P5ZlHfJSjZDMNKP7wJ4xpvwlMWSNzLIuCQ6xVfSe5U3edgjdyJ17tJXiVjO8QlSa+O7nqDrEg/vgixfHGLCn87A7a1w/soZb41MNGjbvqgYPHtGWaq4A6IDkJ9bCzc5UKwc8XCAlqGYe5DrVc+bZFV3w1uYKGd+a802AnhAgMBAAECggEADMhL5SdHaRxHzO0yb9yArxyTNnfK1rr5ordyAgWoAjj3x7n6K2tb/hH2qLksUsOUouP0MIEao1nQ0dAv2domzo93JhFHdn0BQdu8jc4u114ie8b6tjRr/0FnWoFNj1/O7D05zQKcyVQjcx2JMsFohV6DEgMP7CYwnCvMcJEt7OjxWM3fZWwRIhsoVlvsr/VKVhYcyE984r0FOOfPCSbSRIaXpktzISoF7FqhD78Gg9NP/X7kBhCtcWi5+fje4RXcRdp5KEBJmIdv/jFS8x/tC3z/I/CDlW+vicaGeYbRe/NHPIJPwYCh1gAwgNeZwO/grlNRS+QXklEDpPVlABYTLQKBgQC3KWkfw3Czd0PE0sj+QEJYRZpLi1Q6wC0rTVccQOjgpl7lYKPANDbUw0DwTWHyY26lvvAhLgHQijyxXfMeOLAKuqoNCyF95TI/Q5qaxjm9gXFyXHD6jLtz85OeEOw3XbVZF42beQn/SLWyA33RpaDPoK1WsX/uOdCTBsYFFDbsBwKBgQC2ofqz3A4xvuEX70D4/pIApFx/5CPGY16ABPZgPwDpUWvtKpHdg+dCTND00UFPW0A/jO2kECe9+fz4kPAO2eHKX9ICq3QY6rkV37WmT4ETHjH6HL0SpsuPjEo7rIyo5ua8x2WeW+9VCyC2r4e6C5hsscGug7tsvFiN0CRzzTWw1wKBgEN9wdvPc3bd6p3bIe72FLxcIWfW1lmzuLEtFX02udkV1P/mpVBMrSunKwWhLgdX9kyi1+8R+d8zIPwmZ1BqiQvbDpAx2sx+KQ7AxXZktqAK05fVUlgwhV5Lkch76d7Y69sBX8+7QSDNVQ5cVP9sbZ40UcsRKtfIe//BTjRO55H/AoGATv38a3LQiN+giDOpjIi/KZvtjjtk5dfw9bovvi1B/aiD3867zG3SxrBm6JzhfKthPJt95ao/JkJMEk7OJf5DVL7hHNp/mo9bgoSCEu7sSxKSVzW66ahO/0WUjf7xz9QfFKPB5h6pg8UOXzXIpMWDJfA0b4D2ve8w9hWCzruBb30CgYBoPDr0CiASs7NZ4OT/fWzCcudLuHoZDlvGQKFUmQgsiymum0a8bKtMbs3uLl+uwy+gRdSvFf7Juo1fZlnSE46nGQfzet0A03ICa/gtebH6gMpotsvOx5ZsSojDVE9kF6caKBsieKoQHyq37hAWr2GaXHWOJKXl/Dd4SL+7jeqQFg==";

    // 支付宝公钥,对应APPID下的支付宝公钥。
    public String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgqtVJtH7Tbvp2KrhfqjudGkITrR2TC5+pmWxtpe9jKMo4KTpkwxlKc5vMkRTDgMXOAwaNpkQB9g8M6GrL/UqPGMZGSJIRDixP/hFEB8sGzE6vEt9ZFAcQi6qBiz3H1Imk1pJUstSiSlHuhRgpUKd1v3mtFXZjfXjQ4pOhQk+apO/Z3NT+WZR3yUo2QzDSj+8CeMab8JTFkjcyyLgkOsVX0nuVN3nYI3cide7SV4lYzvEJUmvju56g6xIP74IsXxxiwp/OwO2tcP7KGW+NTDRo276oGDx7RlmquAOiA5CfWws3OVCsHPFwgJahmHuQ61XPm2RVd8NbmChnfmvNNgJ4QIDAQAB";

    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notifyUrl = "http://doorbath.natapp1.cc/notify_url.html";
//    public String notifyUrl = "http://doorbath.natapp1.cc/api/alipay/notify";

    //同步通知，支付成功，一般跳转到成功页
    public String returnUrl = "http://doorbath.natapp1.cc/return_url.html";
//    public String returnUrl = "http://member.zyz.com/memberOrder.html";

    // 签名方式
    private String signType = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";
    // 支付宝网关；https://openapi-sandbox.dl.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 支付
     * @param vo
     * @return
     * @throws AlipayApiException
     */
    public String pay(AlipayVO vo) throws
            AlipayApiException {

        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new
                DefaultAlipayClient(gatewayUrl, appId, merchantPrivateKey,
                "json", charset, alipayPublicKey, signType);

        //2、创建一个支付请求，并设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);

//        Long id = order.getId();
        //商户订单号,商户网站订单系统中唯一订单号,必填
        String orderSn = vo.getOutTradeNo();
//        Long interfaceInfoId = order.getInterfaceInfoId();
        //TODO 支付金额,暂时用打赏金额代替,后期改
        String rewardPrice = vo.getTotalAmount();
        //订单名称,必填
        String subject = vo.getSubject();
        //商品描述.可空
        String body = vo.getBody();
//        BigDecimal aggregate = order.getAggregate();  //支付金额
//        String paymentMethod = order.getPaymentMethod();   //支付方式


//        alipayRequest.setBizContent(" {\"out_trade_no\":\"" + id + "\","
//                + "\"total_amount\":\"" + money + "\","
//                + "\"subject\":\"" + interfaceInfoId
//                + "\","
//                + "\"body\":\"" + paymentMethod + "\","
//                +
//                "\"timeout_express\":\"" + timeout + "\","
//                +
//                "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

                alipayRequest.setBizContent(" {\"out_trade_no\":\"" + orderSn + "\","
                + "\"total_amount\":\"" + rewardPrice + "\","
                +
                "\"timeout_express\":\"" + timeout + "\","
                +
                "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

//        String result = alipayClient.pageExecute(alipayRequest).getBody();
        String result = alipayClient.sdkExecute(alipayRequest).getBody();
        String apiUrl = gatewayUrl + "?" + result;

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应：" + result);
//        return result;
        return apiUrl;
    }

}
